﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AFI.Speargun.Web.Models
{
    public class EntitySpeargunRepository : ISpeargunRepository
    {
        private SpeargunEntities _entities = new SpeargunEntities();

        #region Category methods

        public Category CreateCategory(Category category)
        {
            _entities.AddToCategory(category);
            _entities.SaveChanges();

            return category;
        }

        public void DeleteCategory(Category category)
        {
            _entities.DeleteObject(category);
            _entities.SaveChanges();
        }

        public Category EditCategory(Category category)
        {
            var originalCategory = this.GetCategory(category.CategoryId);

            // Save changes
            _entities.ApplyPropertyChanges(originalCategory.EntityKey.EntitySetName, category);
            _entities.SaveChanges();
            return category;
        }

        public Category GetCategory(Guid categoryId)
        {
            return (from c in _entities.Category
                    where c.CategoryId == categoryId
                    select c).First();
        }

        #endregion

        #region Department methods

        public Department CreateDepartment(Department department)
        {
            _entities.AddToDepartment(department);
            _entities.SaveChanges();

            return department;
        }

        public void DeleteDepartment(Department department)
        {
            _entities.DeleteObject(department);
            _entities.SaveChanges();
        }

        public Department EditDepartment(Department department)
        {
            var originalCategory = this.GetDepartment(department.DepartmentId);

            // Save changes
            _entities.ApplyPropertyChanges(originalCategory.EntityKey.EntitySetName, department);
            _entities.SaveChanges();
            return department;
        }

        public Department GetDepartment(Guid departmentId)
        {
            return (from d in _entities.Department
                    where d.DepartmentId == departmentId
                    select d).First();
        }

        #endregion

        #region AssetEntry methods

        public AssetEntry CreateAssetEntry(AssetEntry assetEntry)
        {
            _entities.AddToAssetEntry(assetEntry);
            _entities.SaveChanges();

            return assetEntry;
        }

        public void DeleteAssetEntry(AssetEntry assetEntry)
        {
            _entities.DeleteObject(assetEntry);
            _entities.SaveChanges();
        }

        public AssetEntry EditAssetEntry(AssetEntry assetEntry)
        {
            AssetEntry originalAssetEntry = this.GetAssetEntry(assetEntry.AssetEntryId);

            // Save changes
            _entities.ApplyPropertyChanges(originalAssetEntry.EntityKey.EntitySetName, assetEntry);
            _entities.SaveChanges();
            return assetEntry;
        }

        public AssetEntry GetAssetEntry(Guid assetEntryId)
        {
            return (from a in _entities.AssetEntry
                    where a.AssetEntryId == assetEntryId
                    select a).First();
        }

        #endregion

        #region AssetEntryAdditional methods

        public AssetEntryAdditional CreateAssetEntryAdditional(AssetEntryAdditional assetEntryAdditional)
        {
            _entities.AddToAssetEntryAdditional(assetEntryAdditional);
            _entities.SaveChanges();

            return assetEntryAdditional;
        }

        public void DeleteAssetEntryAdditional(AssetEntryAdditional assetEntryAdditional)
        {
            _entities.DeleteObject(assetEntryAdditional);
            _entities.SaveChanges();
        }

        public AssetEntryAdditional EditAssetEntryAdditional(AssetEntryAdditional assetEntryAdditional)
        {
            AssetEntryAdditional originalAssetEntryAdditional = this.GetAssetEntryAdditional(assetEntryAdditional.AssetEntryId);

            // Save changes
            _entities.ApplyPropertyChanges(originalAssetEntryAdditional.EntityKey.EntitySetName, assetEntryAdditional);
            _entities.SaveChanges();
            return assetEntryAdditional;
        }

        public AssetEntryAdditional GetAssetEntryAdditional(Guid assetEntryId)
        {
            return (from a in _entities.AssetEntryAdditional
                    where a.AssetEntryId == assetEntryId
                    select a).First();
        }

        #endregion

        #region Currency methods

        public Currency CreateCurrency(Currency currency)
        {
            _entities.AddToCurrency(currency);
            _entities.SaveChanges();

            return currency;
        }

        public void DeleteCurrency(Currency currency)
        {
            _entities.DeleteObject(currency);
            _entities.SaveChanges();
        }

        public Currency EditCurrency(Currency currency)
        {
            var originalCurrency = this.GetCurrency(currency.CurrencyId);

            // Save changes
            _entities.ApplyPropertyChanges(originalCurrency.EntityKey.EntitySetName, currency);
            _entities.SaveChanges();
            return currency;
        }

        public Currency GetCurrency(Guid currencyId)
        {
            return (from c in _entities.Currency
                    where c.CurrencyId == currencyId
                    select c).First();
        }

        public IEnumerable<Currency> ListCurrencies()
        {
            return _entities.Currency.ToList();
        }

        #endregion

        #region AcquisitionMode methods

        public AcquisitionMode CreateAcquisitionMode(AcquisitionMode acquisitionMode)
        {
            _entities.AddToAcquisitionMode(acquisitionMode);
            _entities.SaveChanges();

            return acquisitionMode;
        }

        public void DeleteAcquisitionMode(AcquisitionMode acquisitionMode)
        {
            _entities.DeleteObject(acquisitionMode);
            _entities.SaveChanges();
        }

        public AcquisitionMode EditAcquisitionMode(AcquisitionMode acquisitionMode)
        {
            var originalCurrency = this.GetAcquisitionMode(acquisitionMode.AcquisitionModeId);

            // Save changes
            _entities.ApplyPropertyChanges(originalCurrency.EntityKey.EntitySetName, acquisitionMode);
            _entities.SaveChanges();
            return acquisitionMode;
        }

        public AcquisitionMode GetAcquisitionMode(Guid acquisitionModeId)
        {
            return (from a in _entities.AcquisitionMode
                    where a.AcquisitionModeId == acquisitionModeId
                    select a).First();
        }

        public IEnumerable<AcquisitionMode> ListAcquisitionModes()
        {
            return _entities.AcquisitionMode.ToList();
        }

        #endregion

        #region InventoryChecking methods

        public InventoryChecking CreateInventoryChecking(InventoryChecking inventoryChecking)
        {
            _entities.AddToInventoryChecking(inventoryChecking);
            _entities.SaveChanges();

            return inventoryChecking;
        }

        public void DeleteInventoryChecking(InventoryChecking inventoryChecking)
        {
            _entities.DeleteObject(inventoryChecking);
            _entities.SaveChanges();
        }

        public InventoryChecking EditInventoryChecking(InventoryChecking inventoryChecking)
        {
            var originalInventoryChecking = this.GetInventoryChecking(inventoryChecking.InventoryCheckingId);

            // Save changes
            _entities.ApplyPropertyChanges(originalInventoryChecking.EntityKey.EntitySetName, inventoryChecking);
            _entities.SaveChanges();
            return inventoryChecking;
        }

        public InventoryChecking GetInventoryChecking(Guid inventoryCheckingId)
        {
            return (from i in _entities.InventoryChecking
                    where i.InventoryCheckingId == inventoryCheckingId
                    select i).First();
        }

        public IEnumerable<InventoryChecking> ListInventoryCheckings()
        {
            return (from i in _entities.InventoryChecking
                    orderby i.CreateDate descending
                    select i);
        }
        #endregion

        #region AssetInventory methods

        public AssetInventory CreateAssetInventory(AssetInventory assetInventory)
        {
            _entities.AddToAssetInventory(assetInventory);
            _entities.SaveChanges();

            return assetInventory;
        }

        public void DeleteAssetInventory(AssetInventory assetInventory)
        {
            _entities.DeleteObject(assetInventory);
            _entities.SaveChanges();
        }

        public AssetInventory EditAssetInventory(AssetInventory assetInventory)
        {
            AssetInventory originalAssetInventory = this.GetAssetInventory(assetInventory.AssetInventoryId);

            // Save changes
            _entities.ApplyPropertyChanges(originalAssetInventory.EntityKey.EntitySetName, assetInventory);
            _entities.SaveChanges();
            return assetInventory;
        }

        public AssetInventory GetAssetInventory(Guid assetInventoryId)
        {
            return (from a in _entities.AssetInventory
                    where a.AssetInventoryId == assetInventoryId
                    select a).First();
        }

        public IEnumerable<AssetInventory> ListAssetInventories(Guid assetEntryId)
        {
            return (from a in _entities.AssetInventory
                    where a.AssetEntry.AssetEntryId == assetEntryId
                    orderby a.CheckingDate descending
                    select a).ToList();
        }

        #endregion
    }
}
